//===============================================================================
// Microsoft patterns & practices
// Web Client Software Factory
//-------------------------------------------------------------------------------
// Copyright (C) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//-------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================
using System;
using System.Collections.Generic;

namespace WCSFContrib.PageFlow.Tests.Mocks
{
	internal class MockPageFlowInstanceStore : Dictionary<Type, IPageFlow>, IPageFlowInstanceStore
	{
		private Guid _lastRunningInstance;

        public MockPageFlowInstanceStore()
        {
        }

		public void Add(IPageFlow pageFlowInstance)
		{
			base.Add(pageFlowInstance.Definition.PageFlowType, pageFlowInstance);
			_lastRunningInstance = pageFlowInstance.Id;
		}

		public void Remove(IPageFlow pageFlowInstance)
		{
			base.Remove(pageFlowInstance.Definition.PageFlowType);
			_lastRunningInstance = Guid.Empty;
		}

		public void Remove(Guid id)
		{
			List<IPageFlow> existingFlows = new List<IPageFlow>(base.Values);

			IPageFlow targetToDelete = existingFlows.Find(new Predicate<IPageFlow>(delegate(IPageFlow flow)
															{
																return flow.Id == id;
															}));
			if (targetToDelete != null)
			{
				base.Remove(targetToDelete.Definition.PageFlowType);
			}

			_lastRunningInstance = Guid.Empty;
		}

		public Guid GetLastRunningInstance()
		{
            return _lastRunningInstance;
		}

		public Guid GetByType(Type type)
		{
			if (!base.ContainsKey(type))
				return Guid.Empty;

			return base[type].Id;
		}

		public void SetPageFlowRunning(IPageFlow iPageFlow)
		{
			_lastRunningInstance = iPageFlow.Id;
		}

		public void SetPageFlowNotRunning(IPageFlow iPageFlow)
		{
			_lastRunningInstance = Guid.Empty;
		}
     
        public string GetInstanceType(Guid id)
        {
            foreach (KeyValuePair<Type, IPageFlow> pair in this)
            {
                if (pair.Value.Id.Equals(id))
                {
                    return pair.Key.AssemblyQualifiedName;
                }
            }
            return null;
        }
    }
}
